package com.whu.waterworks.service;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.sun.org.apache.regexp.internal.recompile;
import com.whu.waterworks.dao.StationnumpositionDAO;
import com.whu.waterworks.dao.WaterPlantDao;
import com.whu.waterworks.model.Stationnumposition;
import com.whu.waterworks.model.WaterPlant;

public class WaterPlantService {
	private WaterPlantDao waterPlantDao;
	private boolean flag=false;	
	private StationnumpositionDAO stationnumpositionDAO;
	
	public StationnumpositionDAO getStationnumpositionDAO() {
		return stationnumpositionDAO;
	}

	public void setStationnumpositionDAO(StationnumpositionDAO stationnumpositionDAO) {
		this.stationnumpositionDAO = stationnumpositionDAO;
	}

	public List<WaterPlant> getAllWaterPlants() {
		return waterPlantDao.getAll();
	}
	
	public WaterPlant getWaterPlantById(String waterPlantCode) {
		return waterPlantDao.getWaterPlantById(waterPlantCode);
	}
	
	public WaterPlant getWaterPlantByName(String waterPlantName) {
		return waterPlantDao.getWaterPlantById(waterPlantName);
	}

	public WaterPlantDao getWaterPlantDao() {
		return waterPlantDao;
	}

	public void setWaterPlantDao(WaterPlantDao waterPlantDao) {
		this.waterPlantDao = waterPlantDao;
	}
	
	public String addwaterPlant(WaterPlant waterPlant){
    	String message=""; 		
    	System.out.println("提交开始");
		if(null!=waterPlant.getWaterPlantCode() && !waterPlant.getWaterPlantCode().equals("")){
			flag=waterPlantDao.updateWaterPlant(waterPlant);				
		}
		else{
			//新增
			waterPlant.setWaterPlantCode(createwaterPlantCode());
			flag=waterPlantDao.saveWaterPlant(waterPlant);				
		}			
		if(flag){message="提交成功！";}
		else{message="提交失败，请稍后！";}
		return message;
    }
    
    public String getInitDataOfwaterPlant(){
    	
		 String jonsStrOfName="";		
		 JSONArray jArray = new JSONArray();
		 JSONObject jsonObj;
		 List<WaterPlant> rswaterPlant=waterPlantDao.getAll();			
		 for(int i=0;i<rswaterPlant.size();++i){
			 jsonObj=new JSONObject();
			 jsonObj.put("id", rswaterPlant.get(i).getWaterPlantCode());
			 jsonObj.put("name", rswaterPlant.get(i).getWaterPlantName());
			 jArray.add(jsonObj);
		 }
		System.out.println(rswaterPlant.size());
		WaterPlant waterPlant=waterPlantDao.getWaterPlantOfMaxCode();		
		jsonObj=JSONObject.fromObject(waterPlant);
		jArray.add(jsonObj);
		jonsStrOfName=jArray.toString();
		return jonsStrOfName;
    }
    
    public String delWaterPlant(WaterPlant waterPlant){
    	 String message="";
    	// String waterPlantCode=waterPlant.getWaterPlantCode().substring(0,6)+waterPlant.getWaterPlantCode().substring(9,12);
    	 if(null!=stationnumpositionDAO.findAll(waterPlant.getWaterPlantCode())){
  			Stationnumposition wp=new Stationnumposition();
  			message="水厂有监测点数据，不允许删除！";
  			System.out.println("delWaterPlant end");
  			return message;
  		 }    	 
    	 flag=waterPlantDao.delWaterPlant(waterPlant);
		 if(flag){	 
			 message="删除成功！";
		 }
		 else{			 
			 message="删除失败，请稍后重试！";
		 }
		 System.out.println("delWaterPlant end");
		 return message;
    }
    
    //给水库编码4205-8300-0003
    public String createwaterPlantCode(){
    	StringBuilder code=new StringBuilder("42058300");    	
    	String maxCode="";
    	int temp=0;    	
		//maxCode=irrBRStadiaDao.getCreatewaterPlantCode(code.toString());
    	maxCode=waterPlantDao.getWaterPlantOfMaxCode().getWaterPlantCode();
		if(null==maxCode){
			code.append("0001");
			return code.toString();
		}
		String pro="0000";
		temp=Integer.valueOf(maxCode.substring(8))+1;		
		int tempLen=String.valueOf(temp).length();
		code.append(pro.substring(0, 4-tempLen));	
		code.append(temp);
		System.out.println(code.toString());
		return code.toString();    	
    }   
    
	public String getSelectOfWaterPlant(){
		 JSONArray jArray = new JSONArray();
		 JSONObject jsonObj;
		 ArrayList<WaterPlant> rswaterGate=(ArrayList<WaterPlant>) waterPlantDao.getAll();
		 System.out.println(rswaterGate.size());
		 for(int i=0;i<rswaterGate.size();++i){				
			 jsonObj=new JSONObject();
			 jsonObj.put("id", rswaterGate.get(i).getWaterPlantCode().trim());
			 jsonObj.put("name", rswaterGate.get(i).getWaterPlantName().trim());
			 jArray.add(jsonObj);
		 }
		 return  jArray.toString();		
	}
    
    
	public boolean savewaterPlant(WaterPlant waterPlant){
		return waterPlantDao.saveWaterPlant(waterPlant);
	}
	
	public boolean updatewaterPlant(WaterPlant waterPlant){
		return waterPlantDao.updateWaterPlant(waterPlant);
	}	
	
	
	public WaterPlant getWaterPlantOfMaxCode(){		
		return waterPlantDao.getWaterPlantOfMaxCode();
	}
	
	
	/**
	 * 反冲洗水泵个数
	 * @author wangying
	 * @return
	 */
	public int getBackwashPumpNum(String waterPlantCode)
	{
		return waterPlantDao.getBackwashPumpNum(waterPlantCode);
	}
	/**
	 * 反冲洗风机个数
	 * @author wangying
	 * @return
	 */
	public int getBackwashFanNum(String waterPlantCode)
	{
		return waterPlantDao.getBackwashFanNum(waterPlantCode);
	}
	/**
	 * 供水泵个数
	 * @author wangying
	 * @return
	 */
	public int  getSupplyPumpNum(String waterPlantCode) 
	{
		return waterPlantDao.getSupplyPumpNum(waterPlantCode);
	}
	/**
	 * 取水泵个数
	 * @author wangying
	 * @return
	 */
	public int  getGetPumpNum(String waterPlantCode) 
	{
		return waterPlantDao.getGetPumpNum(waterPlantCode);
	}
}
